package net.adetech.crj.utils;

import com.alibaba.fastjson.JSONObject;
import net.adetech.framework.entity.ServiceRequest;
import net.adetech.framework.entity.ServiceResponse;
import net.adetech.framework.utils.SerializeUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InnerServiceUtil {

    @Value("${mid_service.url}")
    public String midServiceUrl;
    @Value("${devMode}")
    private boolean devMode;

    public ServiceResponse call(ServiceRequest request)
    {
        String requestString = SerializeUtil.objectToString(request);
        try {

            StringEntity entity = new StringEntity("{data:'"+requestString+"'}","UTF-8");
            entity.setContentType("application/json");

            HttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(midServiceUrl);
            post.setEntity(entity);


            //post.setHeader("Authorization","APPCODE f77530e7d1324c3bbd47c819aa33711c");
            if (!devMode) {
                post.setHeader("COLLAGEN-SESSION_ID", "no-session");
                post.setHeader("COLLAGEN-REQUESTER_ID", "SD.YTGA.APP.GA.CRJZNYD");
                post.setHeader("COLLAGEN-PROXY_FLOW_ID", "SD.YTGA::01::FLOW_C3_CALL_RESTFUL_PROXY");
                post.setHeader("COLLAGEN-AUTHORIZE_ID", "2a03387cfe4245ce993df05ee3d10e5f");
                post.setHeader("COLLAGEN-TIMEOUT", "30S");
                post.setHeader("COLLAGEN-DEBUG", "OFF");
                post.setHeader("Content-Type", "application/json");
                post.setHeader("COLLAGEN-OUT_PARAMETERS", "_ALL_");
            }

            HttpResponse httpResponse = client.execute(post);
            String responseString = EntityUtils.toString(httpResponse.getEntity());


            JSONObject jsonObject = JSONObject.parseObject(responseString);
            String resultString = jsonObject.getString("result");
            ServiceResponse serviceResponse = (ServiceResponse) SerializeUtil.stringToObject(resultString);
            return serviceResponse;
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return  null;
    }


    public ServiceResponse callService(String serviceName,Object... parameters)
    {
        ServiceRequest request = new ServiceRequest();
        Map<String,Object> params = new HashMap<String, Object>();
        request.setServiceName(serviceName);
        for (int i =0;i<parameters.length;i+=2)
        {
            String key = (String)parameters[i];
            Object value = parameters[i+1];
            params.put(key,value);
        }
        request.setParams(params);
        return  this.call(request);
    }


    public ServiceResponse call(String serviceName,ServiceRequest request)
    {
        request.setServiceName(serviceName);
        return call(request);
    }

    public static Map<String,Object> buildResult(Object... params)
    {
        Map<String,Object> resultMap = new HashMap<String, Object>();
        for (int i=0;i<params.length;i+=2)
        {
            resultMap.put((String)params[i],params[i+1]);

        }
        return resultMap;
    }
}
